from flask_restful import Resource
from flask_restful import reqparse
from flask_restful import abort, marshal_with
from flask_restful import fields, marshal

from apps.apis.movies.utils import get_user
from apps.exts import cache
from apps.utils import generate_movie_user_token

'''
http_ok = 200
http_create_ok = 201

user_action_login = login
user_action_register = register
'''
from apps.models.movie import MovieUser

parse_base = reqparse.RequestParser()
parse_base.add_argument('password', type=str, required=True, help='请输入密码')
parse_base.add_argument('action', type=str, required=True, help='请确认登录还是注册')

parse_register = parse_base.copy()
parse_register.add_argument('username', type=str, required=True, help='请输入用户名')
parse_register.add_argument('email', type=str, required=True, help='请输入QQ邮箱')
parse_register.add_argument('phone', type=str, required=True, help='请输入手机号')

parse_login = parse_base.copy()
parse_login.add_argument('username', type=str, help='请输入用户名')
parse_login.add_argument('email', type=str, help='请输入QQ邮箱')
parse_login.add_argument('phone', type=str, help='请输入手机号')


movie_user_fields = {
    'username': fields.String,
    'password': fields.String(attribute='_password'),
    'phone': fields.String,
    'email': fields.String,
}
single_movie_user_fields = {
    'status': fields.String,
    'msg': fields.String,
    'data': fields.Nested(movie_user_fields),
}




class MovieUsersResource(Resource):

    def post(self):

        args = parse_base.parse_args()

        password = args.get("password")
        action = args.get("action").lower()

        if action == 'register':
            args_register = parse_register.parse_args()
            phone = args_register.get("phone")
            username = args_register.get("username")

            movie_user = MovieUser()

            movie_user.username = username
            movie_user.password = password
            movie_user.phone = phone

            if not movie_user.save():
                abort(400, msg="create fail")

            data = {
                "status": 200,
                "msg": "用户创建成功",
                "data": movie_user
            }

            return marshal(data, single_movie_user_fields)
        elif action == 'login':

            args_login = parse_login.parse_args()

            username = args_login.get("username")
            phone = args_login.get("phone")

            user = get_user(username) or get_user(phone)

            if not user:
                abort(400, msg="用户不存在")

            if not user.check_password(password):
                abort(401, msg="密码错误")

            if user.is_delete:
                abort(401, msg="用户不存在")

            token = generate_movie_user_token()

            cache.set(token, user.id, timeout=60*60*24*7)

            data = {
                "msg": "login success",
                "status": 201,
                "token": token
            }

            return data

        else:
            abort(400, msg="其提供正确的参数")
